const Actions = require('./actions');
import { monStorage, Storage } from 'clientConfig/util/StoreData';
import {NEW, RUNNING, TERMINATED,COMPLETED, CANCELED} from 'clientConfig/constants'
import { Control } from 'react-keeper'
const DB = require('app/db.js');
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {

    },

    onFetch: function (pageNum, props, success, error) {
        let t = this;
        DB.Approval.get_approval_list({
            status: NEW | RUNNING|COMPLETED|CANCELED|TERMINATED,//NEW|RUNNING
            PageNum: pageNum,
            CountForAPage: props.pageSize,
            originator_user_id: monStorage.get('user.id'),
            module_type: Control.state.origin.split('/')[0],
            proj_category: 'changereq',
            proj_name: props.proj_name ? props.proj_name : "",
            proj_num: props.proj_num ? props.proj_num : ""
        })
            .then(function (content) {
                var list = [];
                if (content.list.length > 0) {
                    for (let key in content.list) {
                        var obj = {};
                        obj["badge"] = false;
                        obj["title"] = content.list[key].proj_name;
                        obj["subtitle"] = content.list[key].create_time;
                        obj["process_id"] = content.list[key].process_id;
                        let option = content.list[key].status == COMPLETED ? "审批通过" :(content.list[key].status == CANCELED?"已撤销":(content.list[key].status == TERMINATED?"审批拒绝":"待审批"));
                        let color = option=="审批通过"?"green":(option=="审批拒绝"?"red":"");
                        if (Control.state.origin.split('/')[0] == "tender") {
                            obj["content"] = [
                                { "subcontent": "项目编号", "unitcontent": content.list[key].proj_num },
                                { "subcontent": option, "className": "" }
                            ];
                        } else if (Control.state.origin.split('/')[0] == "workplan") {
                            obj["content"] = [
                                { "subcontent": "项目编号", "unitcontent": content.list[key].proj_num },
                                { "subcontent": "合同签订时间", "unitcontent": content.list[key].proj_create_time },
                                { "subcontent": option, "className": color }
                            ];
                        } else if (Control.state.origin.split('/')[0] == "achievements") {
                            obj["content"] = [
                                { "subcontent": "项目编号", "unitcontent": content.list[key].proj_num },
                                { "subcontent": "成果名称", "unitcontent": content.list[key].proj_create_time },
                                { "subcontent": option, "className": color }
                            ];
                        } else if (Control.state.origin.split('/')[0] == "payment") {
                            if (content.list[key].proj_category == "payplan") {
                                obj["content"] = [
                                    { "subcontent": "项目编号", "unitcontent": content.list[key].proj_num },
                                    { "subcontent": "支付节点", "unitcontent": content.list[key].paymanage_node },
                                    { "subcontent": option, "className": color }
                                ];
                            } else {
                                obj["content"] = [
                                    { "subcontent": "项目编号", "unitcontent": content.list[key].proj_num },
                                    { "subcontent": "所属支付计划", "unitcontent": content.list[key].proj_name },
                                    { "subcontent": option, "className": color }
                                ];
                            }

                        } else if (Control.state.origin.split('/')[0] == "maintenance") {
                            if(content.list[key].proj_category == "faultreq"){
                                obj["content"] = [
                                    { "subcontent": "项目编号", "unitcontent": content.list[key].proj_num },
                                    { "subcontent": "故障单号", "unitcontent": content.list[key].process_id },
                                    { "subcontent": option, "className": color }
                                ];
                            }else{
                                obj["content"] = [
                                    { "subcontent": "项目编号", "unitcontent": content.list[key].proj_num },
                                    { "subcontent": "审批编号", "unitcontent": content.list[key].process_id },
                                    { "subcontent": "故障单号", "unitcontent": content.list[key].proj_falutNum },
                                    { "subcontent": option, "className": color }
                                ];
                            }
                        }
                        list.push(obj);
                    }
                }
                const hasNoMore = list.length < props.pageSize;
                t.data.listData = list;
                if (props.proj_name || props.proj_num)
                    t.updateComponent()
                if (success)
                    success(hasNoMore, list);
            })
            .catch(function (error) {

            });
    },

    updateComponent: function () {
        this.trigger(this.data);
    }
});
